package com.mentalsurvey.service;

import com.mentalsurvey.entity.Paper;
import com.mentalsurvey.entity.Score;
import com.mentalsurvey.entity.ScoreWithPaperDetails;
import com.mentalsurvey.mapper.PaperMapper;
import com.mentalsurvey.mapper.ScoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScoreService {

    @Autowired
    private ScoreMapper scoreMapper;


    @Autowired
    private PaperMapper paperMapper;

    public int saveScore(Score score) {
        return scoreMapper.insertScore(score);
    }

    public List<ScoreWithPaperDetails> getScoreWithPaperDetails(int userId) {
        List<Score> scores = scoreMapper.getScoreByUserId(userId);
        List<ScoreWithPaperDetails> result = new ArrayList<>();

        for (Score score : scores) {
            Paper paper = paperMapper.getPaperById(score.getPaperId());
            if (paper != null) {
                result.add(new ScoreWithPaperDetails(
                        score.getTotalScore(),
                        score.getAnswers(),
                        paper.getTitle(),
                        paper.getPaperType()
                ));
            }
        }

        return result;
    }
}

